# Direct outputs eligible for public release

# GLOBAL SETTINGS --------------------------------------------------------------

options(
  scipen = 999,
  digits = 16,
  max.print = .Machine$integer.max,
  show.error.locations = TRUE,
  warn = 1
)

RNGkind("L'Ecuyer-CMRG")
seed <- 818675309L
set.seed(seed) # setting main seed

# PACKAGES ---------------------------------------------------------------------

library(data.table)

# PACKAGE SETTINGS -------------------------------------------------------------

# data.table
setDTthreads(threads = 1)
options(datatable.print.class = TRUE, datatable.print.keys = TRUE)
# so that printing the data.table also shows the variable type on top

# BEGIN FILE -------------------------------------------------------------------

# Just in case something masks 'source()', use extra deliberate base::source().
base::source("~/code/0-utility-functions/table-and-text-utils.R", local = TRUE)

# READ IN OUR DATA -------------------------------------------------------------

summary_dt <-
    readRDS("~/summary-output/covariates_all_winners_vs_tax_filer_dt.rds")
rel_quartile_dt <-
    readRDS("~/summary-output/quartile_population_shares.rds")

# MAKE TABLE -------------------------------------------------------------------

filedir <- "~/paper/tables/table-2.1.tex"
file.create(filedir)

# Start Table 2.1
start_latex_table(column_structure = "lccc", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "toprule", file_dir = filedir, append = TRUE)
add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

heading_text_input1 <-
    " &  & \\multicolumn{2}{c}{Population} "
write_latex_table_row(heading_text_input1, file_dir = filedir, append = TRUE)

add_latex_cmidrule(
    point_start_col = "l",
    point_end_col = "r",
    start_col = 3,
    end_col = 4,
    file_dir = filedir,
    append = TRUE
)

heading_text_input2 <-
    " \\multirow{2}{*}{Covariate} & \\multirow{2}{*}{Statistic} & Winners (Age 21-64) & Tax Filers (Age 21-64) "
write_latex_table_row(heading_text_input2, file_dir = filedir, append = TRUE)

heading_text_input3 <- "  &  & (1) & (2) "
write_latex_table_row(heading_text_input3, file_dir = filedir, append = TRUE)

heading_text_input1 <- NULL
heading_text_input2 <- NULL
heading_text_input3 <- NULL
rm(heading_text_input1, heading_text_input2, heading_text_input3)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

# Add body of Table 2.1
wage_earnings_input <-
  sprintf(
    "Wage Earnings & Mean & \\$%s & \\$%s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$mean_wages,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$mean_wages,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(wage_earnings_input, file_dir = filedir, append = TRUE)
wage_earnings_input <- NULL
rm(wage_earnings_input)

employment_input <-
  sprintf(
    "Employment & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$mean_employment,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$mean_employment,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(employment_input, file_dir = filedir, append = TRUE)
employment_input <- NULL
rm(employment_input)

age_input <-
  sprintf(
    "Age & Mean & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$mean_age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$mean_age,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(age_input, file_dir = filedir, append = TRUE)
age_input <- NULL
rm(age_input)

gender_input <-
  sprintf(
    "Female & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$mean_female,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$mean_female,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(gender_input, file_dir = filedir, append = TRUE)
gender_input <- NULL
rm(gender_input)

marital_status_input <-
  sprintf(
    "Married & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$mean_married,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$mean_married,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(marital_status_input, file_dir = filedir, append = TRUE)
marital_status_input <- NULL
rm(marital_status_input)

homeowner_input <-
  sprintf(
    "Homeowner & Prop. & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$mean_home,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$mean_home,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(homeowner_input, file_dir = filedir, append = TRUE)
homeowner_input <- NULL
rm(homeowner_input)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

rel_q1_share_input <-
  sprintf(
    "\\multicolumn{2}{c}{Relative Q1 AGI Share} & %s & 0.25",
    fmt_fixed_decimal(
      input = rel_quartile_dt[population_quartile == 1L]$mean_prop,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(rel_q1_share_input, file_dir = filedir, append = TRUE)
rel_q1_share_input <- NULL
rm(rel_q1_share_input)

rel_q2_share_input <-
  sprintf(
    "\\multicolumn{2}{c}{Relative Q2 AGI Share} & %s & 0.25",
    fmt_fixed_decimal(
      input = rel_quartile_dt[population_quartile == 2L]$mean_prop,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(rel_q2_share_input, file_dir = filedir, append = TRUE)
rel_q2_share_input <- NULL
rm(rel_q2_share_input)

rel_q3_share_input <-
  sprintf(
    "\\multicolumn{2}{c}{Relative Q3 AGI Share} & %s & 0.25",
    fmt_fixed_decimal(
      input = rel_quartile_dt[population_quartile == 3L]$mean_prop,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(rel_q3_share_input, file_dir = filedir, append = TRUE)
rel_q3_share_input <- NULL
rm(rel_q3_share_input)

rel_q4_share_input <-
  sprintf(
    "\\multicolumn{2}{c}{Relative Q4 AGI Share} & %s & 0.25",
    fmt_fixed_decimal(
      input = rel_quartile_dt[population_quartile == 4L]$mean_prop,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 2,
      decimal_digits = 2,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(rel_q4_share_input, file_dir = filedir, append = TRUE)
rel_q4_share_input <- NULL
rm(rel_q4_share_input)

add_latex_table_rule(rule_type = "midrule", file_dir = filedir, append = TRUE)

population_size_input <-
  sprintf(
    "\\multicolumn{2}{c}{$N$} & %s & %s",
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "winner_dt"]$total_count,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    ),
    fmt_fixed_decimal(
      input = summary_dt[dt_name == "tax_filer_dt"]$total_count,
      sub_for_missing = "-",
      should_be_rounded = TRUE,
      round_digits = 0,
      decimal_digits = 0,
      scale_factor = NA,
      add_comma = TRUE
    )
  )
write_latex_table_row(population_size_input, file_dir = filedir, append = TRUE)
population_size_input <- NULL
rm(population_size_input)

# End Table 2.1
add_latex_table_rule(
  rule_type = "bottomrule",
  file_dir = filedir,
  append = TRUE
)
end_latex_table(file_dir = filedir, append = TRUE)
filedir <- NULL
rm(filedir)

# Housekeeping
summary_dt <- NULL
rel_quartile_dt <- NULL
rm(summary_dt, rel_quartile_dt)